/**
 * Created by HaiHui on 2017/10/20.
 */
$(document).ready(function() {
    var dataId = GetQueryString("id");

    var dataName = decodeURI(GetQueryString("name"), "utf-8");

    $("#infoDustName").text(dataName);
    $("#hideDustId").val(dataId);

    var setting = {
        url: "dust/graveInit",
        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;
            var regionList = data.regionList;
            var rowList = data.rowList;
            var graveList = data.graveList;
            var regionId = data.regionId;
            var rowId = data.rowId;
            //区list
            for(var i = 0; i < regionList.length; i++ ){
                var region = regionList[i];

                if(i == 0){
                    var html =" <li class='active' onmouseover='show(this)' onmouseout='hide(this)' onclick='regionClick(\""+region.regionName+"\","+region.regionId+")'> <p><em class='fa fa-object-group dogo-css-areaem'></em>"+region.regionName+"<span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRegion("+"this,"+region.regionId+")'></i></span></p></li>";
                }else{
                    var html =" <li  onmouseover='show(this)' onmouseout='hide(this)' onclick='regionClick(\""+region.regionName+"\","+region.regionId+")'> <p><em class='fa fa-object-group dogo-css-areaem'></em>"+region.regionName+"<span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRegion("+"this,"+region.regionId+")'></i></span></p></li>";
                }

                $("#regionList").append(html);
            }
            if(regionList.length > 0){
                $("#hideRegionId").val(regionId);

                $("#infoOnRegionName").text(data.regionName);
                //排列表
                for(var j = 0;j < rowList.length; j++){
                    var row = rowList[j];
                    if(j == 0){
                        var html = "<li onclick='rowClick(\""+row.rowName+"\","+row.rowId+")' onmouseover='show(this)' onmouseout='hide(this)' class='active'> <p><em class='fa fa-tasks dogo-css-areaem'></em>"+row.rowName+"排 <span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRow("+"this,"+row.rowId+")'></i></span></p></li>";
                    }else{
                        var html = "<li onclick='rowClick(\""+row.rowName+"\","+row.rowId+")' onmouseover='show(this)' onmouseout='hide(this)'> <p><em class='fa fa-tasks dogo-css-areaem'></em>"+row.rowName+"排 <span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRow("+"this,"+row.rowId+")'></i></span></p></li>";
                    }
                    $("#rowList").append(html);
                }
                $("#infoOnRowName").text(data.rowName);
                $("#hideRowId").val(rowId);
                //穴列表
                if(rowList.length > 0){
                    for(var m = 0; m < graveList.length; m++){

                        var grave = graveList[m];
                        var html = "";
                        if(grave.useFlg == '0'){
                             html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'>登记</p></div></div>"
                        }else{
                             html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave dogo-css-grave1'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'><a href='index.html'>详情</a></p></div></div>"
                        }

                        $('#graveList').append(html);
                    }
                }

            }


        });
    }, "获取项目详情失败");


});
function regionClick(name,id){
    $("#infoOnRegionName").text(name);
    $("#hideRegionId").val(id);
    refreshRow(id);
}
function rowClick(name,id){
    //墓穴刷新
    $("#infoOnRowName").text(name);
    $("#hideRowId").val(id);
    refreshGrave(id);

}
function graveClick(id){
    $("#hideClickGrave").val(id);
}
function addRow(){
    $("#rowName").val("");

    $("#regionIdByRow").val($("#hideRegionId").val());

    $("#addRowModal").modal('show');

}
function show(thisObj){
    $(thisObj).children().children("span").show();

}
function hide(thisObj){
    $(thisObj).children().children("span").hide();
}
function addGrave(){
    $("#graveName").val("");
    $("#rowIdByGrave").val($("#hideRowId").val());
    $("#addGraveModal").modal('show');

}
function deleteRow(thisObj,id){
    var confirmIdx = layer.confirm("确认要删除?", {
            btn: ['删除', '取消']
        },
        function() {
            var setting = {
                url: "dust/deleteRow",
                data: {
                    id: id
                }
            }
            ajaxSubmit(setting, function(responseData) {
                checkSubmitResult(responseData, {
                    error: "删除失败"
                }, function(responseData) {
                    layer.alert(responseData.description);
                    //动态删除li标签
                    var li = thisObj.parentNode.parentNode; //获得当前行
                    var ul = li.parentNode;
                    ul.removeChild(li);


                });
            }, "删除失败");
        },
        function() {
            layer.close(confirmIdx);
        });
}
function deleteGrave(){
    var graveId = $("#hideClickGrave").val();
    var rowId = $("#hideRowId").val();
    var dustId=$("#hideDustId").val();
    var confirmIdx = layer.confirm("确认要删除?", {
            btn: ['删除', '取消']
        },
        function() {
            var setting = {
                url: "dust/deleteGrave",
                data: {
                    id: graveId,
                    dustId:dustId
                }
            }
            ajaxSubmit(setting, function(responseData) {
                checkSubmitResult(responseData, {
                    error: "删除失败"
                }, function(responseData) {
                    layer.alert(responseData.description);
                    refreshGrave(rowId);
                   /* //动态删除li标签
                    var li = thisObj.parentNode.parentNode; //获得当前行
                    var ul = li.parentNode;

                    ul.removeChild(li);*/


                });
            }, "删除失败");
        },
        function() {
            layer.close(confirmIdx);
        });

}
//刷新墓穴列表
function refreshGrave(id){

    var setting = {
        url: "dust/selectGraves",
        data: {
            id:id
        },
        dataType:'json'

    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "添加失败"
        }, function(responseData) {
            $("#graveList").html("");

            var data = responseData.data;

            if(data.length > 0){
                for(var m = 0; m < data.length; m++){
                    var grave = data[m];
                    var html = "";
                    if(grave.useFlg == '0'){
                        html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'>登记</p></div></div>"
                    }else{
                        html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave dogo-css-grave1'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'><a href='index.html'>详情</a></p></div></div>"
                    }

                    $('#graveList').append(html);
                }
            }

            //layer.alert(responseData.description);
            //refreshRegion(dustId)



        });

    }, "添加失败");

}
$("#saveRowInfo").click(function () {
    var regionId =$("#regionIdByRow").val();
    var rowName = $("#rowName").val();

    var setting = {
        url: "dust/addRow",
        data: {
            regionId:regionId,
            rowName:rowName
        },
        dataType:'json'

    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "添加失败"
        }, function(responseData) {
            $("#addRowModal").modal('hide');
            //layer.alert(responseData.description);
            //refreshRegion(dustId)
            refreshRow(regionId);


        });

    }, "添加失败");

})
function refreshRow(regionId){
    var setting = {
        url: "dust/selectRows",
        data: {
            id:regionId
        },
        dataType:'json'

    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "添加失败"
        }, function(responseData) {
            $("#rowList").find("li").remove();
            $("#graveList").html("");
            var data = responseData.data;
            var rowList = data.rowList;
            var graveList = data.graveList;


            for(var j = 0;j < rowList.length; j++){
                var row = rowList[j];
                if(j == 0){
                    var html = "<li onclick='rowClick(\""+row.rowName+"\","+row.rowId+")' onmouseover='show(this)' onmouseout='hide(this)' class='active'> <p><em class='fa fa-tasks dogo-css-areaem'></em>"+row.rowName+"排 <span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRow("+"this,"+row.rowId+")'></i></span></p></li>";
                }else{
                    var html = "<li onclick='rowClick(\""+row.rowName+"\","+row.rowId+")' onmouseover='show(this)' onmouseout='hide(this)'> <p><em class='fa fa-tasks dogo-css-areaem'></em>"+row.rowName+"排 <span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRow("+"this,"+row.rowId+")'></i></span></p></li>";
                }
                $("#rowList").append(html);
            }
            if(rowList.length > 0){
                $("#infoOnRowName").text(data.rowName);
                $("#hideRowId").val(data.rowId);
                //穴列表
                if(rowList.length > 0){
                    for(var m = 0; m < graveList.length; m++){
                        var grave = graveList[m];
                        var html = "";
                        if(grave.useFlg == '0'){
                            html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'>登记</p></div></div>"
                        }else{
                            html += "<div class='col-lg-2' onclick='graveClick("+grave.graveId+")'><div class='dogo-css-grave dogo-css-grave1'><p class='dogo-css-gravetxt1'>"+grave.graveName+"号</p><p class='dogo-css-gravetxt'><a href='index.html'>详情</a></p></div></div>"
                        }

                        $('#graveList').append(html);
                    }
                }
            }


            //layer.alert(responseData.description);
            //refreshRegion(dustId)



        });

    }, "添加失败");


}
$("#saveGraveInfo").click(function () {
    var rowId =$("#rowIdByGrave").val();
    var graveName = $("#graveName").val();
    var dustId=$("#hideDustId").val();

    var setting = {
        url: "dust/addGrave",
        data: {
            rowId:rowId,
            graveName:graveName,
            dustId:dustId
        },
        dataType:'json'

    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "添加失败"
        }, function(responseData) {
            $("#addGraveModal").modal('hide');
            layer.alert(responseData.description);
            refreshGrave(rowId);



        });

    }, "添加失败");

})

function addRegion(){
    $("#regionName").val("");
    $("#dustIdByRegion").val($("#hideDustId").val());
    $("#addRegionModel").modal('show');
}
$("#saveRegionInfo").click(function () {
    var dustId =$("#dustIdByRegion").val();
    var regionName = $("#regionName").val();

    var setting = {
        url: "dust/addRegion",
        data: {
            dustId:dustId,
            regionName:regionName
        },
        dataType:'json'

    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "添加失败"
        }, function(responseData) {
            $("#addRegionModel").modal('hide');
            layer.alert(responseData.description);
            refreshRegion(dustId)



        });

    }, "添加失败");

})
//刷新区数据
function refreshRegion(id){
    $("#regionList").find("li").remove();

    var setting = {
        url: "dust/selectRegions",
        data: {
            id: id
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取项目详情失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var data = responseData.data;

            for(var i = 0; i < data.length; i++ ){
                var region = data[i];
                if(i == 0){
                    var html =" <li class='active' onmouseover='show(this)' onmouseout='hide(this)' onclick='regionClick(\""+region.regionName+"\","+region.regionId+")'> <p><em class='fa fa-object-group dogo-css-areaem'></em>"+region.regionName+"<span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRegion("+"this,"+region.regionId+")'></i></span></p></li>";
                }else{
                    var html =" <li  onmouseover='show(this)' onmouseout='hide(this)' onclick='regionClick(\""+region.regionName+"\","+region.regionId+")'> <p><em class='fa fa-object-group dogo-css-areaem'></em>"+region.regionName+"<span style='display: none'><i class='fa fa-trash-o'  style='margin-left: 30px;' onclick='deleteRegion("+"this,"+region.regionId+")'></i></span></p></li>";
                }


                $("#regionList").append(html);
            }

            $("#hideDustId").val(id);


        });
    }, "获取项目详情失败");


}
function deleteRegion(thisObj,id){

    var confirmIdx = layer.confirm("确认要删除?", {
            btn: ['删除', '取消']
        },
        function() {
            var setting = {
                url: "dust/deleteRegion",
                data: {
                    id: id
                }
            }
            ajaxSubmit(setting, function(responseData) {
                checkSubmitResult(responseData, {
                    error: "删除失败"
                }, function(responseData) {
                    layer.alert(responseData.description);
                    //动态删除li标签
                    var li = thisObj.parentNode.parentNode; //获得当前行
                    var ul = li.parentNode;
                    ul.removeChild(li);


                });
            }, "删除失败");
        },
        function() {
            layer.close(confirmIdx);
        });


}
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}